package com.system.controller;

import com.system.pojo.*;
import com.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 陈文杰 on 2022/6/9 10:03
 */
@Controller
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    @Autowired
    EmployeeDimissionService employeeDimissionService;

    @Autowired
    EmplAttendanceService emplAttendanceService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    EmployeeSalaryService employeeSalaryService;

    @Autowired
    DepartmentSalaryService departmentSalaryService;

    @RequestMapping("/toDepartmentMember")
    public ModelAndView toDepartmentMember(String departmentName) {
        ModelAndView modelAndView = new ModelAndView();
        List<Employee> employees = employeeService.queryEmployeeByEmpDeptName(departmentName);
        modelAndView.addObject("deptEmployees", employees);
        modelAndView.setViewName("emp");
        return modelAndView;
    }

    @RequestMapping("/toEmployees")
    public ModelAndView toEmployees() {
        ModelAndView modelAndView = new ModelAndView();
        List<Employee> employees = employeeService.queryEmployeeAll();
        modelAndView.addObject("deptEmployees", employees);
        modelAndView.setViewName("emp");
        return modelAndView;
    }

    @RequestMapping("/toAddEmp")
    public String toAddEmp() {
        return "addEmp";
    }

    @RequestMapping("/addEmp")
    public String addEmp(String empName,
                         int empAge,
                         String empSex,
                         String empPhone,
                         String empPost,
                         String empDeptName) {
        Employee employee = new Employee();
        Department department = departmentService.queryDeptByName(empDeptName);
        int deptId = department.getDeptId();
        employee.setEmpDeptId(deptId);
        employee.setEmpAge(empAge);
        employee.setEmpDeptName(empDeptName);
        employee.setEmpName(empName);
        employee.setEmpSex(empSex);
        employee.setEmpPhone(empPhone);
        employee.setEmpPost(empPost);
        employeeService.addEmployeeAll(employee);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("deptName", empDeptName);
        stringObjectHashMap.put("deptNumber", department.getDeptNumber() + 1);
        departmentService.updateDepartmentByName(stringObjectHashMap);


        //同步出勤表员工数据
        EmplAttendance emplAttendance = new EmplAttendance();
        int empId = employeeService.queryEmployeeByEmpName(empName).getEmpId();
        emplAttendance.setEmpId(empId);
        emplAttendance.setEmpName(empName);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String format = simpleDateFormat.format(date);
        emplAttendance.setEmpDate(format);
        emplAttendance.setEmpIsAtt(0);
        emplAttendanceService.addEmplAttendance(emplAttendance);

        //同步员工工资表信息
        EmployeeSalary employeeSalary = new EmployeeSalary();
        employeeSalary.setEmpName(empName);
        employeeSalary.setEmpId(empId);
        employeeSalary.setDeptName(empDeptName);
        employeeSalary.setEmpSalDate(format);
        DepartmentSalary departmentSalary = departmentSalaryService.queryDepartmentSalaryByDeptName(empDeptName);
        if (empPost.equals("技术岗"))
            employeeSalary.setEmpBasePay(departmentSalary.getDeptBasePay() + departmentSalary.getDeptTechnologySal());
        if (empPost.equals("管理岗"))
            employeeSalary.setEmpBasePay(departmentSalary.getDeptBasePay() + departmentSalary.getDeptManageSal());
        if (empPost.equals("普通岗"))
            employeeSalary.setEmpBasePay((departmentSalary.getDeptBasePay() + departmentSalary.getDeptCommonSal()));
        if (empPost.equals("实习岗"))
            employeeSalary.setEmpBasePay(departmentSalary.getDeptBasePay() + departmentSalary.getDeptPracticeSal());
        employeeSalary.setEmpAttSal(departmentSalary.getDeptAttSal());
        employeeSalary.setEmpDeductSal(departmentSalary.getDeptAttSal());
        employeeSalary.setEmpPracticalSal(employeeSalary.getEmpBasePay()+employeeSalary.getEmpAttSal()-employeeSalary.getEmpInsurance());
        employeeSalaryService.addEmployeeSalary(employeeSalary);
        return "redirect:toEmployees";
    }


    @RequestMapping("/toChangeEmp")
    public ModelAndView changeEmp(int empId) {
        ModelAndView modelAndView = new ModelAndView();
        Employee employee = employeeService.queryEmployeeByEmpId(empId);
        System.out.println(employee.getEmpName());
        modelAndView.addObject("changeEmployee", employee);
        modelAndView.setViewName("changeEmp");
        return modelAndView;
    }

    @RequestMapping("/changeEmp")//修改员工信息
    public ModelAndView changeEmp(int empId,
                                  String empName,
                                  int empAge,
                                  String empSex,
                                  String empPhone,
                                  String empPost,
                                  String empDeptName) {
        Employee employee = employeeService.queryEmployeeByEmpId(empId);
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> map = new HashMap<>();
        map.put("empId", empId);
        if (!employee.getEmpName().equals(empName)) map.put("empName", empName);
        if (employee.getEmpAge() != empAge) map.put("empAge", empAge);
        if (!employee.getEmpSex().equals(empSex)) map.put("empSex", empSex);
        if (!employee.getEmpPhone().equals(empPhone)) map.put("empPhone", empPhone);
        if (!employee.getEmpPost().equals(empPost)) map.put("empPost", empPost);
        if (!employee.getEmpDeptName().equals(empDeptName)) map.put("empDeptName", empDeptName);
        employeeService.updateEmployeeByEmpId(map);
        List<Employee> employees = employeeService.queryEmployeeAll();
        modelAndView.addObject("deptEmployees", employees);
        modelAndView.setViewName("emp");
       /* Employee employee = new Employee();
        employee.setEmpId(empId);
        employee.setEmpAge(empAge);
        employee.setEmpDeptName(empDeptName);
        employee.setEmpName(empName);
        employee.setEmpSex(empSex);
        employee.setEmpPhone(empPhone);
        employee.setEmpPost(empPost);
        employeeService.updateEmployeeByEmpId(empId);*/


       //同步出勤表 员工工资表
        if (!employee.getEmpName().equals(empName)){
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("empName",empName);
            map1.put("empId",empId);
            emplAttendanceService.updateEmplAttendanceByEmpId(map1);
            employeeSalaryService.updateEmployeeSalaryByEmpId(map1);
        }


        return modelAndView;
    }

    @RequestMapping("/deleteEmp")
    public String deleteEmp(int empId) {//删除员工信息并添加离职员工信息,部门信息员工数量减一
        ModelAndView modelAndView = new ModelAndView();
        EmployeeDimission employeeDimission = new EmployeeDimission();
        Employee employee = employeeService.queryEmployeeByEmpId(empId);
        employeeService.deleteEmployeeByEmpId(empId);

        //获取当前日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String format = simpleDateFormat.format(date);

        //添加离职信息
        employeeDimission.setEmpDimDate(format);
        employeeDimission.setEmpDimDeptName(employee.getEmpDeptName());
        employeeDimission.setEmpId(employee.getEmpId());
        employeeDimission.setEmpName(employee.getEmpName());
        employeeDimission.setEmpPost(employee.getEmpPost());
        employeeDimissionService.addEmployeeDimission(employeeDimission);

        //修改部门表信息
        Department department = departmentService.queryDeptByName(employee.getEmpDeptName());
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("deptName", department.getDeptName());
        stringObjectHashMap.put("deptNumber", department.getDeptNumber() - 1);
        departmentService.updateDepartmentByName(stringObjectHashMap);

        //删除出勤表信息
        emplAttendanceService.deleteEmplAttendanceByEmpId(empId);

        //删除员工工资表信息
        employeeSalaryService.deleteEmployeeSalaryByEmpId(empId);
        return "redirect:flush";
    }

    @RequestMapping("/flush")
    public ModelAndView flush() {
        ModelAndView modelAndView = new ModelAndView();
        List<Employee> employees = employeeService.queryEmployeeAll();
        modelAndView.addObject("deptEmployees", employees);
        modelAndView.setViewName("emp");
        return modelAndView;
    }

    @RequestMapping("/toDimissionEmp")
    public ModelAndView toDimissionEmp() {
        ModelAndView modelAndView = new ModelAndView();
        List<EmployeeDimission> employeeDimissions = employeeDimissionService.queryEmployeeDimissionAll();
        modelAndView.addObject("employeeDimissions", employeeDimissions);
        modelAndView.setViewName("dimissionEmp");
        return modelAndView;
    }


}
